package de.hijacksoft.isle.classes;

import java.util.ArrayList;

/**
 * the Recipe holding information about ingredients, rating, last bought time ...
 * its comparable to make it sortable by rating and lastbought
 * @author HiJack Soft
 */
public class Recipe implements Comparable<Recipe> {

    public String name = "";
    public ArrayList<Item> ingredients = new ArrayList<Item>();
    public double rating = 0;
    public long lastBought = System.currentTimeMillis();
    public int id = -1;

    public Recipe() {
    }

    public Recipe(String name) {
        this.name = name;
    }

    public double getPrice() {
        double price = 0;
        for (int i = 0; i < ingredients.size(); i++) {
            price += ingredients.get(i).price;
        }
        return price;
    }

    public void addItem(Item item) {
        for (int i = 0; i < ingredients.size(); i++) {
            if (ingredients.get(i).name.equals(item.name)) {
                ingredients.get(i).amount += item.amount;
                ingredients.get(i).price += item.price;
                return;
            }
        }
        ingredients.add(item);
    }

    @Override
    public String toString() {
        String items = "";
        for (int i = 0; i < ingredients.size(); i++) {
            items += ingredients.get(i).name;
            items += " " + ingredients.get(i).amount;
            items += ingredients.get(i).unit + ", ";
        }
        return name + ", rating: " + rating + " ( " + items.toString() + " )";
    }

    /*
     * compares two recipes to see which should be rather bought
     * takes the lastBought amount difference in days * 3 and adds the rating difference * 2
     * result:  higher for recipes bought earlier
     *          higher for higher rated recipes
     */
    @Override
    public int compareTo(Recipe another) {
        return ((int) (((another.lastBought - lastBought) / 86400000) * 3) + ((int) (rating - another.rating) * 2));
    }
}
